<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskId">
        <el-select v-model="queryParams.taskId" placeholder="请选择任务" clearable filterable>
          <el-option v-for="e in taskList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属类目" prop="reviewStatus">
        <el-select v-model="queryParams.item" placeholder="请选择所属类目" clearable>
          <el-option v-for="e in sysItemList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['task:report:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['task:report:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['task:report:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['task:report:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="举报编号" align="center" prop="id"/>
      <el-table-column label="任务编号" align="center" prop="taskId"/>
      <el-table-column label="任务名称" align="center" prop="taskTitle">
        <template slot-scope="scope">
          <a @click="handleDetail(scope.row)" style="color: dodgerblue">{{ scope.row.taskTitle }}</a>
        </template>
      </el-table-column>
      <el-table-column label="举报内容" align="center" prop="reportInfo"/>
      <el-table-column label="微信用户" align="center" prop="reportUserName"/>
      <el-table-column label="所属类目" align="center" prop="item">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_item" :value="scope.row.item"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['task:report:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['task:report:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改任务举报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="45%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="任务名称" prop="taskId">
              <el-select v-model="form.taskId" placeholder="请选择任务" clearable filterable>
                <el-option v-for="e in taskList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信用户" prop="taskId">
              <el-select v-model="form.reportUserId" placeholder="请选择微信用户" clearable filterable>
                <el-option v-for="e in wechatUserList" :key="e.dictLabel" :label="e.dictLabel" :value="e.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="举报内容" prop="reportInfo">
              <el-input v-model="form.reportInfo" type="textarea" placeholder="请输入举报内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 任务详情组件 -->
    <TaskDetail :task-detail="taskDetail" :detail-open="detailOpen" @cancelDetail="cancelDetail"></TaskDetail>
  </div>
</template>

<script>
import { addReport, delReport, getReport, listReport, updateReport } from '@/api/task/report'
import { getTask, getTaskDicts } from '@/api/task/task'
import { getWechatUserDictsWithoutSystem } from '@/api/wechat/wechatUser'
import TaskDetail from '@/components/TaskDetail/TaskDetail'

export default {
  components: { TaskDetail },
  name: 'taskReport',
  dicts: ['sys_item'],
  data() {
    return {
      loading: true,                                                        // 遮罩层
      ids: [],                                                              // 选中数组
      single: true,                                                         // 非单个禁用
      multiple: true,                                                       // 非多个禁用
      showSearch: true,                                                     // 显示搜索条件
      total: 0,// 总条数
      reportList: [],                                                       // 任务举报表格数据
      title: '',                                                            // 弹出层标题
      open: false,                                                          // 是否显示弹出层
      queryParams: {                                                        // 查询参数
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        taskTitle: null,
        reportInfo: null,
        reportUserId: null
      },
      taskDetail: {},
      detailOpen: false,
      form: {                                                               // 表单参数
      },
      rules: {                                                              // 表单校验
        taskId: [{ required: true, message: '请选择任务名称', trigger: 'blur' }],
        reportUserId: [{ required: true, message: '请选择微信用户', trigger: 'blur' }],
        reportInfo: [{ required: true, message: '请输入举报内容', trigger: 'blur' }]
      },
      taskList: [],                                                         // 任务列表
      wechatUserList: [],                                                   // 微信用户列表
      sysItemList: []                                                       // 任务类目列表
    }
  },
  created() {
    this.initDict()
    this.getList()
  },
  methods: {
    /** 查询任务举报列表 */
    getList() {
      this.loading = true
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        taskTitle: null,
        reportInfo: null,
        reportUserId: null,
        createTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加任务举报'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改任务举报'
      })
    },
    /** 详情按钮操作 */
    handleDetail: function (row) {
      getTask(row.taskId).then(response => {
        this.taskDetail = response.data
        this.detailOpen = true
      })
    },
    /** 退出详情组件操作 */
    cancelDetail: function (detailOpen) {
      this.detailOpen = detailOpen
      this.taskDetail = {}
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addReport(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除任务举报编号为"' + ids + '"的数据项？').then(function () {
        return delReport(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    },

    /** 初始化字典 */
    initDict: function () {
      getTaskDicts().then(response => {
        this.taskList = response.data
      })
      getWechatUserDictsWithoutSystem().then(response => {
        this.wechatUserList = response.data
      })
      this.getDicts('sys_item').then(response => {
        this.sysItemList = response.data
      })
    }
  }
}
</script>
